/**
 * Qiangungun.com Inc.
 * Copyright (c) 2015-2015 All Rights Reserved.
 */
package com.qiangungun.report.biz.service.action.query;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiangungun.core.enums.BizCode;
import com.qiangungun.report.biz.service.action.AbstractAction;
import com.qiangungun.report.biz.share.ActionContext;
import com.qiangungun.report.common.model.exceptions.RptServiceException;
import com.qiangungun.report.dal.dataobject.RptcfgQueryParamDO;
import com.qiangungun.report.service.model.ServiceRequest;
import com.qiangungun.report.service.model.ServiceResult;
import com.qiangungun.report.service.model.config.RptcfgQueryParam;

/**
 * 从缓存中查找报表查询参数配置
 * @author hangdahong
 * @version $Id: QueryDefineParamFromCacheAction.java, v 0.1 2015年3月6日 下午4:38:58 hangdahong Exp $
 */
public class QueryDefineParamFromCacheAction extends AbstractAction{

    private static final Logger logger = LoggerFactory.getLogger(QueryDefineParamFromCacheAction.class);
    
    /** 
     * @see com.qiangungun.report.biz.share.BaseAction#perform()
     */
    @Override
    public void perform() throws RptServiceException {
        logger.info("开始从缓存中查询报表参数配置");
        ActionContext context=ActionContext.getContext();
        ServiceResult<List<RptcfgQueryParam>> resp =(ServiceResult<List<RptcfgQueryParam>>)context.getResponse();
        ServiceRequest<Long> req=(ServiceRequest<Long>)context.getRequest();
        Long defineId=req.getBody();
        
        if(defineId==null){
            logger.error("报表定义ID不能为空");
            throw new RptServiceException(BizCode.ParamIsNull,"报表定义ID不能为空");
        }
        
        List<RptcfgQueryParamDO> queryParamDOs= this.rptcfgQueryParamCache.get(defineId);
        if(queryParamDOs==null ){
            logger.error("缓存中没有报表查询参数配置");
            throw new RptServiceException(BizCode.BizDataNotExists,"缓存中没有报表查询参数配置");
        }
        
        List<RptcfgQueryParam> params=new ArrayList<RptcfgQueryParam>();
        RptcfgQueryParam rptcfgQueryParam=null;
        for(RptcfgQueryParamDO obj:queryParamDOs){
            rptcfgQueryParam=new RptcfgQueryParam();
            convert(obj,rptcfgQueryParam);
            params.add(rptcfgQueryParam);
        }
        
        resp.setBody(params);
        resp.setCode(BizCode.Success);
        
        logger.info("从缓存中查询报表参数配置完毕{}",resp);
    }
    
    private void convert(RptcfgQueryParamDO queryParamDO,RptcfgQueryParam queryParam){
        queryParam.setId(queryParamDO.getId());
        queryParam.setCheckedValue(queryParamDO.getCheckedValue());
        queryParam.setColWidth(queryParamDO.getColWidth());
        queryParam.setDataType(queryParamDO.getDataType());
        queryParam.setDefaultValue(queryParamDO.getDefaultValue());
        
        queryParam.setDefineId(queryParamDO.getDefineId());
        queryParam.setDisplaySequence(queryParamDO.getDisplaySequence());
        queryParam.setInterfacePara(queryParamDO.getInterfacePara());
        queryParam.setInterfaceUrl(queryParamDO.getInterfaceUrl());
        queryParam.setIsEditable(queryParamDO.getIsEditable());
        queryParam.setIsNullable(queryParamDO.getIsNullable());
        queryParam.setModuleType(queryParamDO.getModuleType());
        queryParam.setParamCnName(queryParamDO.getParamCnName());
        queryParam.setParamEnName(queryParamDO.getParamEnName());
        queryParam.setUncheckedValue(queryParamDO.getUncheckedValue());
    }

}
